﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCH.Areas.Admin.Models;
using DCH.Models;

namespace DCH.Areas.Admin.Controllers
{
      [Authorize]
    public class NewController : Controller
    {
        private DbConnection db = new DbConnection();

        //
        // GET: /Admin/New/

        public ActionResult Index()
        {
            return View(db.New.ToList());
        }

        //
        // GET: /Admin/New/Details/5

        public ActionResult Details(int id = 0)
        {
            News news = db.New.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        //
        // GET: /Admin/New/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/New/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(News news, HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Guid.NewGuid().ToString().Substring(0, 4) + Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/images/player/"), fileName);
                news.imageThumbnail = fileName;
                file.SaveAs(path);
            }
            if (ModelState.IsValid)
            {

                db.New.Add(news);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(news);
        }

        //
        // GET: /Admin/New/Edit/5

        public ActionResult Edit(int id = 0)
        {
            News news = db.New.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        //
        // POST: /Admin/New/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(News news, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = "";
                    if (news.imageThumbnail == null)
                        fileName = Guid.NewGuid().ToString().Substring(0, 4) + Path.GetFileName(file.FileName);
                    else
                        fileName = news.imageThumbnail;
                    var path = Path.Combine(Server.MapPath("~/images/player/"), fileName);

                    file.SaveAs(path);
                }

                db.Entry(news).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(news);
        }

        //
        // GET: /Admin/New/Delete/5

        public ActionResult Delete(int id = 0)
        {
            News news = db.New.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        //
        // POST: /Admin/New/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            News news = db.New.Find(id);
            db.New.Remove(news);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}